<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type">
          <el-option v-for="item in typeList" :key="item"  :value="item">{{ item }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="是否有效" prop="del">
        <el-select v-model="queryParams.del">
          <el-option value="0" label="有效">有效</el-option>
          <el-option value="1" label="无效">无效</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="sj">
        <el-date-picker clearable
          v-model="queryParams.sj"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="处理部门" prop="procDept">
        <el-input
          v-model="queryParams.procDept"
          placeholder="请输入处理部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理方式" prop="procMethed">
        <el-input
          v-model="queryParams.procMethed"
          placeholder="请输入处理方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理内容" prop="procOpinions">
        <el-input
          v-model="queryParams.procOpinions"
          placeholder="请输入处理内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理时间" prop="processTime">
        <el-date-picker clearable
          v-model="queryParams.processTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择处理时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="goAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容" align="center" prop="content" width="350">
        <template slot-scope="scope">
          <div class="artcileContent">
            <el-tooltip class="item" effect="dark" placement="top-start">
              <div slot="content" ><div v-html="scope.row.content" style="width: 400px;"></div></div>
              <div v-html="scope.row.content "></div>
            </el-tooltip>
          </div>
          
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="是否置顶" align="center" prop="isTop">
        <template slot-scope="scope">
          {{ scope.row.isTop|topFliter }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="是否有效" align="center" prop="del" >
        <template slot-scope="scope">
          {{ scope.row.del | delFliter }}
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="sj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="处理部门" align="center" prop="procDept" />
      <el-table-column label="处理方式" align="center" prop="procMethed" />
      <el-table-column label="处理内容" align="center" prop="procOpinions" />
      <el-table-column label="处理时间" align="center" prop="processTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.processTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="status" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button v-if="scope.row.isTop==1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="changeTop(scope.row,0)"
          >取消置顶</el-button>
          <el-button v-else
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="changeTop(scope.row,1)"
          >置顶</el-button>
          <el-button v-if="scope.row.del==1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="changeDel(scope.row,0)"
          >启用</el-button>
          <el-button v-else
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="changeDel(scope.row,1)"
          >禁用</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改大屏列内容，包括闭环管理、部门共治、行业自治、平台协同对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="是否置顶" prop="isTop">
          <el-input v-model="form.isTop" placeholder="请输入是否置顶" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="删除标记" prop="del">
          <el-input v-model="form.del" placeholder="请输入删除标记" />
        </el-form-item>
        <el-form-item label="时间" prop="sj">
          <el-date-picker clearable
            v-model="form.sj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理部门" prop="procDept">
          <el-input v-model="form.procDept" placeholder="请输入处理部门" />
        </el-form-item>
        <el-form-item label="处理方式" prop="procMethed">
          <el-input v-model="form.procMethed" placeholder="请输入处理方式" />
        </el-form-item>
        <el-form-item label="处理内容" prop="procOpinions">
          <el-input v-model="form.procOpinions" placeholder="请输入处理内容" />
        </el-form-item>
        <el-form-item label="处理时间" prop="processTime">
          <el-date-picker clearable
            v-model="form.processTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择处理时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listForBack, getArticle, delArticle, addArticle, updateArticle,changeDel,changeTop } from "@/api/ptsj/article";

export default {
  name: "Article",
  data() {
    return {
      params:{
        id:"",
        isTop:"",
        del:""
      },
      typeList:["部门共治","行业自治","平台协同","图片管理","通知公告"],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大屏列内容，包括闭环管理、部门共治、行业自治、平台协同表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        isTop: null,
        sort: null,
        del: null,
        sj: null,
        procDept: null,
        procMethed: null,
        procOpinions: null,
        processTime: null,
        status: null,
        typeList:["部门共治","行业自治","平台协同","图片管理","通知公告"],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  filters:{

    delFliter(val){
      if(val==1){
        return "无效"
      }else{
        return "有效"
      }
    },
    topFliter(val){
      if(val==1){
        return "置顶"
      }else{
        return "未置顶"
      }
    }

  },
  created() {
    this.getList();
  },
  methods: {

    changeTop(row,isTop){
      this.params.id = row.id;
      this.params.isTop = isTop;
      changeTop(this.params).then(res => {
        if(res.code == 200) {
          this.$message.success('更新成功!');
          this.getList();
        }
      })
    },
    changeDel(row,del){
      this.params.id = row.id;
      this.params.del = del;
      changeDel(this.params).then(res => {
        if(res.code == 200) {
          this.$message.success('更新成功!');
          this.getList();
        }
      })
    },
    /** 查询大屏列内容，包括闭环管理、部门共治、行业自治、平台协同列表 */
    getList() {
      this.loading = true;
      
      listForBack(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        content: null,
        isTop: null,
        sort: null,
        del: null,
        sj: null,
        procDept: null,
        procMethed: null,
        procOpinions: null,
        processTime: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    goAdd() {
      this.$router.push({path: '/bigscreen/article/add', query: { }});
      // this.reset();
      // this.open = true;
      // this.title = "添加大屏列内容，包括闭环管理、部门共治、行业自治、平台协同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({path: '/bigscreen/article/add', query: {id:row.id }});
      // this.reset();
      // const id = row.id || this.ids
      // getArticle(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改大屏列内容，包括闭环管理、部门共治、行业自治、平台协同";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArticle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除大屏列内容，包括闭环管理、部门共治、行业自治、平台协同编号为"' + ids + '"的数据项？').then(function() {
        return delArticle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ptsj/article/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
  .artcileContent{
    -webkit-line-clamp: 2;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 350px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
  }
</style>